package com.flame.parameter.service;

import java.util.List;

import com.flame.parameter.controller.vo.DictionaryItemVo;
import com.flame.parameter.controller.vo.DictionaryVo;
import com.flame.parameter.entity.Dictionary;
import com.flame.parameter.entity.DictionaryItem;
import com.flame.parameter.exception.ParameterExceptionMessage;
import com.flame.parameter.exception.ParameterRuntimeException;
import com.flame.parameter.mapper.DictionaryItemMapper;
import com.flame.parameter.mapper.DictionaryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 *
 * @author wuxintong
 * @since 2022/10/26
 */
@Service
public class DictionaryService {

    @Autowired
    private DictionaryMapper dictionaryMapper;

    @Autowired
    private DictionaryItemMapper dictionaryItemMapper;

    public List<Dictionary> pageDictionary(DictionaryVo dictionaryVo) {
        return dictionaryMapper.page(dictionaryVo);
    }

    public void addDictionary(Dictionary dictionary) {
        Dictionary dict = dictionaryMapper.selectById(dictionary.getDictCode());
        if (dict != null) {
            throw new ParameterRuntimeException(ParameterExceptionMessage.DICT_EXIST, dictionary.getDictCode());
        }
        dictionaryMapper.insert(dictionary);
    }

    public void updateDictionary(Dictionary dictionary) {
        dictionaryMapper.updateById(dictionary);
    }

    public void deleteDictionary(String dictCode) {
        dictionaryMapper.deleteById(dictCode);
    }

    public List<String> queryDictionaryGroup(String dictGroup) {
        return dictionaryMapper.listDictGroup(dictGroup);
    }

    public List<DictionaryItem> pageDictionaryItem(DictionaryItemVo dictionaryItemVo) {
        return dictionaryItemMapper.page(dictionaryItemVo);
    }

    public void addDictionaryItem(DictionaryItem dictionaryItem) {
        DictionaryItem item =
            dictionaryItemMapper.selectById(dictionaryItem.getDictCode(), dictionaryItem.getDictItemValue());
        if (item != null) {
            throw new ParameterRuntimeException(ParameterExceptionMessage.DICT_ITEM_EXIST, dictionaryItem.getDictItemValue());
        }
        dictionaryItemMapper.insert(dictionaryItem);
    }

    public void updateDictionaryItem(DictionaryItem dictionaryItem) {
        dictionaryItemMapper.updateById(dictionaryItem);
    }

    public void deleteDictionaryItem(String dictCode, String dictItemValue) {
        dictionaryItemMapper.deleteById(dictCode, dictItemValue);
    }
}
